package guggaTests.tasks
{
	import flash.events.Event;
	
	import flexunit.framework.TestCase;
	
	import gugga.tasks.ExecuteMethodTask;
	import gugga.tasks.ITask;
	import gugga.tasks.events.TaskEvent;

	public class ExecuteMethodTaskTest extends TestCase
	{
		private var mIsMethodExecuted : Boolean = false;
		private var mMethodArguments : Boolean = false;
		private var mIsExecuteMethodTaskRunning : Boolean = false;
		
		public function ExecuteMethodTaskTest(methodName:String=null)
		{
			super(methodName);
		}
		
		public function testStartComplete() : void
		{
			var task : ITask = ExecuteMethodTask.create(executeMethod, [true]);
			
			task.addEventListener(TaskEvent.START, onTaskStart);
			task.addEventListener(TaskEvent.COMPLETED, addAsync(assertTaskCompleted, 300));
			
			task.start();
			
			assertTrue("ExecuteMethodTask should be running", mIsExecuteMethodTaskRunning);
			
		}
		
		public function testStartInterrupt () : void
		{
			var task : ITask = ExecuteMethodTask.create(executeMethod, [true]);
			
			task.addEventListener(TaskEvent.INTERRUPTED, addAsync(assertTaskInterrupted, 300));
			
			task.start();
			task.interrupt();
		}
		
		private function onTaskStart(aEvent : Event) : void
		{
			var task : ITask = ITask(aEvent.target);
			
			mIsExecuteMethodTaskRunning = task.isRunning();	
		}
		
		private function assertTaskInterrupted(aEvent : Event) : void
		{
			var task : ITask = ITask(aEvent.target);
			
			assertFalse("ExecuteMethodTask should not be running", task.isRunning());
		}
		
		private function assertTaskCompleted(aEvent : Event) : void
		{
			var task : ITask = ITask(aEvent.target);
			
			assertFalse("ExecuteMethodTask should not be running", task.isRunning());
			
			assertTrue("Method arguments should be passed", mMethodArguments);
			assertTrue("Method should be executed", mIsMethodExecuted);
		}
		
		private function executeMethod(aArguments : Boolean) : void
		{
			mMethodArguments = aArguments;
			mIsMethodExecuted = true;
		}		
	}
}